﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;

namespace YArchitech.Revit
{
	public class HVACPipeUpdater : ElementsUpdater
	{
		public HVACPipeUpdater(Autodesk.Revit.DB.Document doc, List<HYUpdateElementInfo> listNodes) : base(doc, listNodes)
		{
		}

		protected override bool Update(string docTitle)
		{
			bool flag = true;
			Transaction transaction = new Transaction(this.document, "更新数据");
			transaction.Start();
			try
			{
				foreach (HYUpdateElementInfo hyupdateElementInfo in this.nodesToUpdate)
				{
					if (!(hyupdateElementInfo.DocumentTitle == docTitle))
					{
						base.FailedString = "图元所属文档与当前文档不匹配";
						flag = false;
						break;
					}
					Element element = this.document.GetElement(new ElementId(hyupdateElementInfo.ElementID));
					if (element != null)
					{
						if (base.CheckCategory(element, BuiltInCategory.OST_PipeCurves))
						{
							flag &= this.UpdateDuctPipe(element, hyupdateElementInfo);
						}
						else if (base.CheckCategory(element, BuiltInCategory.OST_PipeAccessory))
						{
							flag &= this.UpdateDuctAccessory(element, hyupdateElementInfo);
						}
						else if (base.CheckCategory(element, BuiltInCategory.OST_PipeFitting))
						{
							flag &= this.UpdateDuctFitting(element, hyupdateElementInfo);
						}
					}
				}
				this.document.Regenerate();
				flag = ((int)transaction.Commit() == 3);
			}
			catch (Exception ex)
			{
				base.FailedString = ex.ToString();
				flag = false;
			}
			finally
			{
				transaction.GetStatus();
				if (transaction != null && transaction.HasStarted() && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
					flag = false;
				}
			}
			return flag;
		}

		protected bool UpdateDuctPipe(Element ele, HYUpdateElementInfo eleInfo)
		{
			bool result = false;
			if (eleInfo.PipeDN > 0)
			{
				base.SetDistance(ele, BuiltInParameter.RBS_PIPE_DIAMETER_PARAM, (double)eleInfo.PipeDN / 1000.0);
				result = true;
			}
			return result;
		}

		protected bool UpdateConnector(Connector conn, HYUpdateElementInfo eleInfo)
		{
			bool result = false;
			if (conn != null && eleInfo != null)
			{
				try
				{
					conn.Radius = base.DistanceToStd((double)eleInfo.PipeDN / 2000.0);
					result = true;
				}
				catch (Exception ex)
				{
					ex.ToString();
				}
			}
			return result;
		}

		protected bool UpdateDuctAccessory(Element ele, HYUpdateElementInfo eleInfo)
		{
			bool flag = true;
			FamilyInstance familyInstance = ele as FamilyInstance;
			if (familyInstance != null)
			{
				foreach (Connector connector in base.GetElementConnectors(familyInstance))
				{
					if (connector.Domain == (Autodesk.Revit.DB.Domain)3)
					{
						flag &= this.UpdateConnector(connector, eleInfo);
					}
				}
			}
			return flag;
		}

		protected bool UpdateDuctFitting(Element ele, HYUpdateElementInfo eleInfo)
		{
			bool result = false;
			FamilyInstance familyInstance = ele as FamilyInstance;
			MechanicalFitting mechanicalFitting = familyInstance.MEPModel as MechanicalFitting;
			HYUpdateAttachedEleInfo hyupdateAttachedEleInfo = eleInfo as HYUpdateAttachedEleInfo;
			if (familyInstance != null && mechanicalFitting != null && hyupdateAttachedEleInfo != null)
			{
                switch ((int)mechanicalFitting.PartType)
				{
				case 5:
					result = this.UpdateElbow(familyInstance, hyupdateAttachedEleInfo);
					break;
				case 6:
					result = this.UpdateTee(familyInstance, hyupdateAttachedEleInfo);
					break;
				case 7:
					result = this.UpdateTrans(familyInstance, hyupdateAttachedEleInfo);
					break;
				case 8:
					result = this.UpdateCross(familyInstance, hyupdateAttachedEleInfo);
					break;
				}
			}
			return result;
		}

		protected bool UpdateTrans(FamilyInstance fi, HYUpdateAttachedEleInfo attachedEleInfo)
		{
			bool flag = true;
			if (fi != null && attachedEleInfo != null)
			{
				foreach (Connector connector in base.GetElementConnectors(fi))
				{
					Connector connectConnector = base.GetConnectConnector(connector);
					if (connectConnector != null)
					{
						if (attachedEleInfo.InElementInfo != null && connectConnector.Owner.Id.IntegerValue == attachedEleInfo.InElementInfo.ElementID)
						{
							flag &= this.UpdateConnector(connector, attachedEleInfo.InElementInfo);
						}
						if (attachedEleInfo.OutElementInfo != null && connectConnector.Owner.Id.IntegerValue == attachedEleInfo.OutElementInfo.ElementID)
						{
							flag &= this.UpdateConnector(connector, attachedEleInfo.OutElementInfo);
						}
					}
				}
			}
			return flag;
		}

		protected bool UpdateElbow(FamilyInstance fi, HYUpdateAttachedEleInfo attachedEleInfo)
		{
			bool flag = true;
			if (fi != null && attachedEleInfo != null)
			{
				foreach (Connector conn in base.GetElementConnectors(fi))
				{
					flag &= this.UpdateConnector(conn, attachedEleInfo.OutElementInfo);
				}
			}
			return flag;
		}

		protected bool UpdateTee(FamilyInstance fi, HYUpdateAttachedEleInfo attachedEleInfo)
		{
			bool flag = true;
			if (fi != null && attachedEleInfo != null)
			{
				foreach (Connector connector in base.GetElementConnectors(fi))
				{
					Connector connectConnector = base.GetConnectConnector(connector);
					if (connectConnector != null)
					{
						if (attachedEleInfo.InElementInfo != null && connectConnector.Owner.Id.IntegerValue == attachedEleInfo.InElementInfo.ElementID)
						{
							flag &= this.UpdateConnector(connector, attachedEleInfo.InElementInfo);
						}
						if (attachedEleInfo.OutElementInfo != null && connectConnector.Owner.Id.IntegerValue == attachedEleInfo.OutElementInfo.ElementID)
						{
							flag &= this.UpdateConnector(connector, attachedEleInfo.OutElementInfo);
						}
						if (attachedEleInfo.SideElementInfo != null && connectConnector.Owner.Id.IntegerValue == attachedEleInfo.SideElementInfo.ElementID)
						{
							flag &= this.UpdateConnector(connector, attachedEleInfo.SideElementInfo);
						}
					}
				}
			}
			return flag;
		}

		protected bool UpdateCross(FamilyInstance fi, HYUpdateAttachedEleInfo attachedEleInfo)
		{
			bool flag = true;
			if (fi != null && attachedEleInfo != null)
			{
				foreach (Connector connector in base.GetElementConnectors(fi))
				{
					Connector connectConnector = base.GetConnectConnector(connector);
					if (connectConnector != null)
					{
						if (attachedEleInfo.InElementInfo != null && connectConnector.Owner.Id.IntegerValue == attachedEleInfo.InElementInfo.ElementID)
						{
							flag &= this.UpdateConnector(connector, attachedEleInfo.InElementInfo);
						}
						if (attachedEleInfo.OutElementInfo != null && connectConnector.Owner.Id.IntegerValue == attachedEleInfo.OutElementInfo.ElementID)
						{
							flag &= this.UpdateConnector(connector, attachedEleInfo.OutElementInfo);
						}
						if (attachedEleInfo.SideElementInfo != null && connectConnector.Owner.Id.IntegerValue == attachedEleInfo.SideElementInfo.ElementID)
						{
							flag &= this.UpdateConnector(connector, attachedEleInfo.SideElementInfo);
						}
						if (attachedEleInfo.Side2ElementInfo != null && connectConnector.Owner.Id.IntegerValue == attachedEleInfo.Side2ElementInfo.ElementID)
						{
							flag &= this.UpdateConnector(connector, attachedEleInfo.Side2ElementInfo);
						}
					}
				}
			}
			return flag;
		}
	}
}
